package com.jf3q.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.admin.service.impl.TeacherServiceImpl;
import com.jf3q.admin.utils.dto.TeacherQueryDTO;
import com.jf3q.admin.utils.vo.AjaxResultVO;
import com.jf3q.common.domain.EduTeacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author ASUS
 * @date 2024/4/16
 * @apiNote
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    TeacherServiceImpl teacherService;

    @PostMapping("/page")
    public AjaxResultVO page(@RequestBody TeacherQueryDTO teacher,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = ""+Integer.MAX_VALUE) Integer pageSize){
        LambdaQueryWrapper<EduTeacher> teacherWrapper=new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(teacher)){
            if (!ObjectUtils.isEmpty(teacher.getName())){
                teacherWrapper.like(EduTeacher::getName,teacher.getName());
            }
            if (!ObjectUtils.isEmpty(teacher.getLevel())){
                teacherWrapper.eq(EduTeacher::getLevel,teacher.getLevel());
            }
            if (!ObjectUtils.isEmpty(teacher.getBeginDate())||!ObjectUtils.isEmpty(teacher.getEndDate())){
                teacherWrapper.between(EduTeacher::getEduCreate,teacher.getBeginDate(),teacher.getEndDate());
            }
            teacherWrapper.orderByDesc(EduTeacher::getEduModified);
        }
        Page<EduTeacher> page=teacherService.page(new Page<>(pageNum,pageSize),teacherWrapper);
        return AjaxResultVO.success(page);
    }

    @GetMapping("/getById/{id}")
    public AjaxResultVO toUpdate(@PathVariable String id){
        return AjaxResultVO.success(teacherService.getById(id));
    }

    @PostMapping("/saveOrUpdate")
    public AjaxResultVO saveOrUpdate(@RequestBody EduTeacher teacher){
        return teacherService.saveOrUpdate(teacher)?AjaxResultVO.success():AjaxResultVO.error();
    }

    @GetMapping("/delete/{id}")
    public AjaxResultVO delete(@PathVariable String id){
        return teacherService.removeById(id)?AjaxResultVO.success():AjaxResultVO.error();
    }

    @GetMapping("/list")
    public AjaxResultVO list(){
        return AjaxResultVO.success(teacherService.list());
    }

}
